package com.molyfun.parents.activity.signup;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.SignUpEvaluateResponse;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SignUpEvaluateSheetAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private HashMap<String, String> singleChoiceIdList = new HashMap<>();
    private List<SignUpEvaluateResponse.DataBean.SheetRecordsBean> mList;
    private SignUpEvaluateSheetHolder mSignUpEvaluateSheetViewHolder;


    public SignUpEvaluateSheetAdapter(Context context) {
        this.mContext = context;
    }

    public void refreshData(List<SignUpEvaluateResponse.DataBean.SheetRecordsBean> records) {
        this.mList = records;
        notifyDataSetChanged();
    }

    public void clearAll() {
        if (mList != null) {
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<SignUpEvaluateResponse.DataBean.SheetRecordsBean> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SignUpEvaluateSheetHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_sign_up_evaluate, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final SignUpEvaluateResponse.DataBean.SheetRecordsBean recordsBean = mList.get(position);
        mSignUpEvaluateSheetViewHolder = (SignUpEvaluateSheetHolder) holder;

        final String type = recordsBean.getType();//"1"单选，"2"多选
        if(TextUtils.isEmpty(type) || type.equals("1")){
            mSignUpEvaluateSheetViewHolder.mChoiceQuestion.setText(recordsBean.getContent() + "(单选)");
        }else{
            mSignUpEvaluateSheetViewHolder.mChoiceQuestion.setText(recordsBean.getContent() + "(多选)");
        }

        mSignUpEvaluateSheetViewHolder.mAnswerContainer.removeAllViews();
        if(recordsBean.getOption().size() > 0){
            int optionNum = 0;
            for(SignUpEvaluateResponse.DataBean.SheetRecordsBean.OptionBean optionBean : recordsBean.getOption()){
                View view = LayoutInflater.from(mContext).inflate(R.layout.view_include_choose_info, null, false);
                final ImageView questionState = (ImageView) view.findViewById(R.id.question_check_state);
                TextView questionText = (TextView) view.findViewById(R.id.question_text);
                questionText.setText(optionBean.getContent());
                mSignUpEvaluateSheetViewHolder.mAnswerContainer.addView(view);

                final int optionNumCopy = optionNum;
                //1单选
                if (type.equals("1")) {
                    if(singleChoiceIdList.containsKey("id" + position +"recordsBean.getOption().size() =" + recordsBean.getOption().size() + "single") && recordsBean.getOption().get(optionNum).getId().equals(singleChoiceIdList.get("id" + position +"recordsBean.getOption().size() =" + recordsBean.getOption().size() + "single"))) {
                        questionState.setImageResource(R.mipmap.ic_pay_checkbox_choosed);
                    }else {
                        questionState.setImageResource(R.mipmap.ic_pay_checkbox_normal);
                    }
                }else{
                    if(singleChoiceIdList.containsKey("id" + position + optionBean.getSeq())) {
                        questionState.setImageResource(R.mipmap.ic_pay_checkbox_choosed);
                    }else {
                        questionState.setImageResource(R.mipmap.ic_pay_checkbox_normal);
                    }
                }

                questionState.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (type.equals("1")) {
                            questionState.setImageResource(R.mipmap.ic_pay_checkbox_choosed);
                            singleChoiceIdList.put("id" + position +"recordsBean.getOption().size() =" + recordsBean.getOption().size() + "single", recordsBean.getOption().get(optionNumCopy).getId());
                            notifyItemChanged(position);
                        }else{
                            if(singleChoiceIdList.containsKey("id" + position + recordsBean.getOption().get(optionNumCopy).getSeq())) {
                                questionState.setImageResource(R.mipmap.ic_pay_checkbox_normal);
                                singleChoiceIdList.remove("id" + position + recordsBean.getOption().get(optionNumCopy).getSeq());
                            }else {
                                questionState.setImageResource(R.mipmap.ic_pay_checkbox_choosed);
                                singleChoiceIdList.put("id" + position + recordsBean.getOption().get(optionNumCopy).getSeq(), recordsBean.getOption().get(optionNumCopy).getId());
                            }
                        }
                    }
                });
                optionNum ++;
            }
        }else{
            //没有选项
        }
    }

    public class SignUpEvaluateSheetHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.choice_question)
        public TextView mChoiceQuestion;
        @InjectView(R.id.answer_container)
        LinearLayout mAnswerContainer;
        public SignUpEvaluateSheetHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }

    public HashMap<String, String> getSingleChoiceIdList() {
        return singleChoiceIdList;
    }
}
